﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Reflection;

namespace Odelle.Utils
{
    public class ClassInfoReader
    {
        public class Info
        {
            public string Name { set; get; }
            public string Title { set; get; }
            public Type DataType { set; get; }

            public string DataTypeName
            { 
                get { return this.DataType.Name; } 
            }

            public bool IsNumber
            {
                get
                {
                    if (DataType == typeof(int) || DataType == typeof(long) || DataType == typeof(float) || DataType == typeof(double) || DataType == typeof(DateTime))
                        return true;
                    else
                        return false;
                }
            }

            public override string ToString()
            {
                return Name + "/" + DataTypeName;
            }
        }

        private Dictionary<string, Info> infoList = new Dictionary<string, Info>();

        public ClassInfoReader()
        {
        }

        public ClassInfoReader(Type ClassType)
        {
            this.New(ClassType);
        }

        public void New(Type ClassType)
        {
            infoList.Clear();
            Odelle.Utils.CsNameSplitter csname = new Utils.CsNameSplitter();

            System.Reflection.PropertyInfo[] members = ClassType.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);

            for (int i = 0; i < members.Length; ++i)
            {
                if (members[i].MemberType == MemberTypes.Property)
                {
                    if (members[i].CanRead)
                    {
                        Info infodata = new Info();
                        string name = members[i].Name;

                        infodata.Name = name;
                        infodata.Title = csname.Split(name);
                        infodata.DataType = members[i].PropertyType;

                        infoList.Add(name, infodata);
                    }
                }
            }
        }

        public void Clear()
        {
            infoList.Clear();
        }

        public string[] GetNames()
        {
            return infoList.Keys.ToArray<string>();
        }

        public Info GetInfo(string name)
        {
            return infoList[name];
        }

    }
}
